package id.co.shuba.spamobile.task;

import android.os.AsyncTask;
import android.view.View;
import android.widget.ProgressBar;

public class TestDownloadTask extends AsyncTask<String, Integer, AsyncTaskResult<Void>>{

	private ProgressBar _pb;
	
	public TestDownloadTask(ProgressBar pb) {
		_pb = pb;
	}
	
	@Override
	protected AsyncTaskResult<Void> doInBackground(String... params) {
		for (int i = 1; i <= 100; i++) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			publishProgress(i);
		}
		
		return new AsyncTaskResult<Void>((Void)null);
	}
	
	@Override
	protected void onPreExecute() {		
		super.onPreExecute();
		
		_pb.setProgress(0);
		if (_pb.getVisibility() != View.VISIBLE)
			_pb.setVisibility(View.VISIBLE);
	}
	
	@Override
	protected void onProgressUpdate(Integer... values) {
		_pb.setProgress(values[0]);
	}

	@Override
	protected void onPostExecute(AsyncTaskResult<Void> result) {		
		super.onPostExecute(result);
				
		_pb.setVisibility(View.INVISIBLE);
	}
}
